﻿@using Website.CRUDServices
@using ProfileContactType = Website.Models.Domain.ProfileContactType
@using ProfileRelation = Website.Models.Domain.ProfileRelation
@using ProfileSubtype = Website.Models.Domain.ProfileSubtype
@model Website.Models.ViewModels.ProfilesView
@{
    this.ViewBag.Title = "Opdatér profil";
    this.Layout = "~/Views/_Shared/_Layout.cshtml";
    var errorMsg = new HtmlString(string.Empty);
    if (this.ViewData["ErrorMessage"] != null)
    {
        errorMsg = this.ViewData["ErrorMessage"] as HtmlString;
    }

    Profile prof = null;
    if (this.ViewData["Profile"] != null)
    {
        prof = this.ViewData["Profile"] as Profile;
    }

    var isProfileReferenced = this.ViewData["IsProfileReferenced"] as ProfileIsReferencedResult ?? new ProfileIsReferencedResult();
}

@Html.Partial("~/Views/_Shared/LogView.cshtml", prof.ID)


@using (this.Html.BeginForm())
{
    <div class="error">
        @errorMsg
    </div>
    <input type="hidden" name="ID" value="@prof.ID" />

    <div class="formBullet">Navn*:</div>
    <div class="formEntry">
        <input type="text" name="Name" tabindex="1" autofocus="autofocus" value="@prof.Name" />
    </div>

    <div class="formBullet">CVR:</div>
    <div class="formEntry">
        <input type="text" name="CVR" tabindex="2" value="@prof.CVR"  />
    </div>

    <div class="formBullet">Relation*:</div>
    <div class="formEntry">
        <select name="Relation" tabindex="3">
            <option value="@Guid.Empty">Vælg en relation</option>
            @foreach (ProfileRelation rel in this.Model.Relations.OrderBy(x => x.Type))
            {
                string selected = rel.ID.Equals(prof.Relation) ? " selected=\"selected\" " : string.Empty;
                <option value="@rel.ID" @selected>@rel.Type</option>
            }
        </select>
    </div>

    <div class="formBullet">Kontakt type:</div>
    <div class="formEntry">
        <select name="ContactType" tabindex="4">
            <option value="@Guid.Empty">Vælg en kontakt-type</option>
            @foreach (ProfileContactType c in this.Model.ContactTypes.OrderBy(x => x.Type))
            {
                string selected = c.ID.Equals(prof.ContactType) ? " selected=\"selected\" " : string.Empty;
                <option value="@c.ID" @selected>@c.Type</option>
            }
        </select>
    </div>

    <div class="formBullet">Sektor og type*:</div>
    <div class="formEntry">
        <select name="SubType" tabindex="5">
            <option value="@Guid.Empty">Vælg en type</option>
            @foreach (ProfileSubtype typ in this.Model.Subtypes.OrderBy(x => x.Type.Sector.Name).ThenBy(x => x.Type.Name).ThenBy(x => x.Name))
            {
                string selected = typ.ID.Equals(prof.SubType) ? " selected=\"selected\" " : string.Empty;
                <option value="@typ.ID" @selected>
                    @typ.Type.Sector.Name - 
                    @typ.Type.Name
                    @if (!typ.Name.Equals(string.Empty))
                    {
                        @Html.Label(string.Format(" - {0}", typ.Name))
                    }

                </option>
            }
        </select>
    </div>

   
    <div class="formBullet">Inden for 150km?</div>
    <div class="formEntry">
        <input type="radio" name="Within150Km" value="true" @if (prof.Within150Km ?? false)
                                                            { @Html.Raw("checked");
                                                            } />Ja<br />
        <input type="radio" name="Within150Km" value="false" @if (!(prof.Within150Km ?? false))
                                                             { @Html.Raw("checked");
                                                             }/>Nej
    </div>  
    <div class="formBullet">Er iværksætter?</div>
    <div class="formEntry">
        <input type="radio" name="IsEntrepreneur" value="true" @if (prof.IsEntrepreneur.HasValue && prof.IsEntrepreneur.Value)
                                                            { @Html.Raw("checked");
                                                            } />Ja<br />
        <input type="radio" name="IsEntrepreneur" value="false" @if (!prof.IsEntrepreneur.HasValue || !prof.IsEntrepreneur.Value)
                                                             { @Html.Raw("checked");
                                                             }/>Nej
    </div>
    
    <div class="formBullet">
        Region:
    </div>
    <div class="formEntry">
        <select name="Region">
            <option value="">Vælg en region</option>
            @foreach (Website.Models.Domain.Region reg in Model.Regions.OrderBy(x => x.Country.Name).ThenBy(x => x.Name))
            {
                <option value="@reg.ID"
                    @if (prof.Region == reg.ID)
                    { @Html.Raw("selected=\"selected\"");
                    }
                    >@reg.Country.Name - @reg.Name</option>
            }
        </select>
    </div>
    
    <div class="formBullet">Ved valg af Region Syddanmark, er det i udkantsområde?</div>
    <div class="formEntry">
        <input type="radio" name="IsRemoteArea" value="true" @if (prof.IsRemoteArea.HasValue && prof.IsRemoteArea.Value)
                                                             { @Html.Raw("checked");
                                                             } />Ja<br />
        <input type="radio" name="IsRemoteArea" value="false" @if (!prof.IsRemoteArea.HasValue || !prof.IsRemoteArea.Value)
                                                              { @Html.Raw("checked");
                                                              }/>Nej
    </div>

    
    if (isProfileReferenced.IsReferenced)
    {
        
        <div class="formBullet">
            Aktiviteter med denne profil:<br/>
            <label style="font-size:small;font-style:normal;">(En profil kan ikke slettes hvis den er brugt i aktiviteter)</label>
        </div>
        <div class="formEntry">
            
            @foreach (var pRef in isProfileReferenced.List.OrderBy(x => x.Item2))
            {
                <div style="clear:both;margin-top:10px;">
                    @if (pRef.Item2 == LogEntryType.BDA)
                    {
                        @Html.ActionLink("Forretningsudvikling", "ViewBusinessDevelopment", "BusinessDevelopment", new { Id = pRef.Item1 }, new { target = pRef.Item1.ToString() })
                    }
                    else if (pRef.Item2 == LogEntryType.NA)
                    {
                        @Html.ActionLink("Netværk", "ViewNetwork", "Networking", new { Id = pRef.Item1 }, new { target = pRef.Item1.ToString() })
                    }
                    else if (pRef.Item2 == LogEntryType.Matchmaking)
                    {
                        @Html.ActionLink("Matchmaking", "ViewMatchmaking", "Matchmaking", new { Id = pRef.Item1 }, new { target = pRef.Item1.ToString() })
                    }
                    else if (pRef.Item2 == LogEntryType.Cluster)
                    {
                        @Html.ActionLink("Klynge", "ViewCluster", "Cluster", new { Id = pRef.Item1 }, new { target = pRef.Item1.ToString() })
                    }
                    else if (pRef.Item2 == LogEntryType.ProjectMeeting)
                    {
                        @Html.ActionLink("Projekt", "ViewProject", "Projects", new { Id = pRef.Item1 }, new { target = pRef.Item1.ToString() })
                    }
                </div>
            }
            
        </div>
    }
    <div class="buttonHolder">
        @if (!isProfileReferenced.IsReferenced)
        {
            <script type="text/javascript">
                function confirmDelete() {
                    if (confirm("Er du sikker på at du vil slette profilen? Dette valg kan ikke fortrydes.\n(Hvis profilen indgår i aktiviteter vil den ikke blive slettet.)")) {
                        location.href = '@this.Url.Action("DeleteProfile", new { ID = prof.ID })';
                    }
                }
            </script>
            <input type="button" value="Slet" class="deleteButton margin-right-20"
                   onclick=" confirmDelete(); " />
        }
        <input type="submit" value="Opdatér" tabindex="7" />
    </div>
}
